package com.wenhui.schoolyardinfo.square.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.callback.RcvOnItemItemViewClickListener;
import com.wenhui.schoolyardinfo.customview.CircleImageView;
import com.wenhui.schoolyardinfo.square.bean.SquareRecommendBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/6/22 10:26
 * introduce : 广场的为你推荐Adapter
 */

public class SquareRecommendAdapter extends RecyclerView.Adapter {

    private static final String TAG = "SquareRecommendAdapter";
    private Context context;
    private List<SquareRecommendBean> squareRecommendBeanList = new ArrayList<>();
    private static final int FOOTR_VIEW_TYPE = 1;
    private static final int CONTENT_VIEW_TYPE = 2;

    public SquareRecommendAdapter(Context context) {
        this.context = context;
    }

    public SquareRecommendAdapter(Context context, List<SquareRecommendBean> squareRecommendBeanList) {
        this.context = context;
        this.squareRecommendBeanList = squareRecommendBeanList;
    }

    public void clearData() {
        squareRecommendBeanList.clear();
        notifyDataSetChanged();
    }

    public void addAllData(List<SquareRecommendBean> squareRecommendBeanList) {
        this.squareRecommendBeanList.addAll(squareRecommendBeanList);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case FOOTR_VIEW_TYPE:
                view = LayoutInflater.from(context).inflate(R.layout.item_square_recommend_footer, null);
                return new FooterHolder(view);
            default:
                view = LayoutInflater.from(context).inflate(R.layout.item_square_recommend_content, null);
                return new ContentHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FooterHolder) {
            FooterHolder footerHolder = (FooterHolder) holder;
            Glide.with(context).load(squareRecommendBeanList.get(position).getHeadPortraitUrl()).into(footerHolder.imvHeadPortrait);
            footerHolder.tevName.setText(squareRecommendBeanList.get(position).getName());
            footerHolder.tevDynamicAndFans.setText(squareRecommendBeanList.get(position).getDynamicNum() + "条动态 " + squareRecommendBeanList.get(position).getFansNum() + "粉丝");
        } else {
            ContentHolder contentHolder = (ContentHolder) holder;
            Glide.with(context).load(squareRecommendBeanList.get(position).getHeadPortraitUrl()).into(contentHolder.imvHeadPortrait);
            contentHolder.tevName.setText(squareRecommendBeanList.get(position).getName());
            contentHolder.tevDynamicAndFans.setText(squareRecommendBeanList.get(position).getDynamicNum() + "条动态 " + squareRecommendBeanList.get(position).getFansNum() + "粉丝");
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == squareRecommendBeanList.size() - 1) {
            return FOOTR_VIEW_TYPE;
        } else {
            return CONTENT_VIEW_TYPE;
        }
    }

    @Override
    public int getItemCount() {
        return squareRecommendBeanList.size();
    }

    protected class ContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.imv_head_portrait)
        CircleImageView imvHeadPortrait;
        @BindView(R.id.imv_close)
        ImageView imvClose;
        @BindView(R.id.layout_close)
        FrameLayout layoutClose;
        @BindView(R.id.tev_name)
        TextView tevName;
        @BindView(R.id.tev_dynamic_and_fans)
        TextView tevDynamicAndFans;
        @BindView(R.id.tev_concern_add)
        TextView tevConcernAdd;
        @BindView(R.id.layout_concern_add)
        RelativeLayout layoutConcernAdd;
        @BindView(R.id.layout_recommend_content)
        FrameLayout layoutRecommendContent;

        public ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutClose.setOnClickListener(this);
            layoutConcernAdd.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_close:
                    rcvOnItemItemViewClickListener.onItemItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_concern_add:
                    rcvOnItemItemViewClickListener.onItemItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    protected class FooterHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.imv_head_portrait)
        CircleImageView imvHeadPortrait;
        @BindView(R.id.imv_close)
        ImageView imvClose;
        @BindView(R.id.layout_close)
        FrameLayout layoutClose;
        @BindView(R.id.tev_name)
        TextView tevName;
        @BindView(R.id.tev_dynamic_and_fans)
        TextView tevDynamicAndFans;
        @BindView(R.id.tev_concern_add)
        TextView tevConcernAdd;
        @BindView(R.id.layout_concern_add)
        RelativeLayout layoutConcernAdd;
        @BindView(R.id.layout_recommend_footer)
        FrameLayout layoutRecommendFooter;

        public FooterHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutClose.setOnClickListener(this);
            layoutConcernAdd.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_close:
                    rcvOnItemItemViewClickListener.onItemItemClickListener(getAdapterPosition(), view);
                    break;
                case R.id.layout_concern_add:
                    rcvOnItemItemViewClickListener.onItemItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    private RcvOnItemItemViewClickListener rcvOnItemItemViewClickListener;

    public void setRcvOnItemItemViewClickListener(RcvOnItemItemViewClickListener rcvOnItemItemViewClickListener) {
        this.rcvOnItemItemViewClickListener = rcvOnItemItemViewClickListener;
    }

}
